package com.ydy.data.service.impl;

import com.ydy.common.model.User;
import com.ydy.common.util.CodeUtil;
import com.ydy.common.util.PageHelperUtils;
import com.ydy.common.vo.PageInfo;
import com.ydy.data.mapper.UserInfoMapper;
import com.ydy.data.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    private static Logger LOGGER = LoggerFactory.getLogger(UserInfoServiceImpl.class);


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<User> queryUserInfo(User user, PageInfo pageInfo) {
        PageHelperUtils.startPage(pageInfo);
        return userInfoMapper.queryUserInfo(user,pageInfo);
    }

    @Override
    public void submitUserInfo(User user) {
        String userCode = user.getUserCode();
        if(StringUtils.isEmpty(userCode)){
            //查询
            List<User> userList = userInfoMapper.queryUserInfo(user,new PageInfo());
            if(!CollectionUtils.isEmpty(userList)){
                throw new RuntimeException("该用户名已存在,请重试!");
            }
            //插入
            user.setUserCode(CodeUtil.getCode());
            userInfoMapper.insertUserInfo(user);
        }else{
            //修改
            userInfoMapper.updateUserInfo(user);
        }
    }

    @Override
    @Transactional
    public void deleteUserInfo(String userCode) {
        userInfoMapper.deleteUserInfo(userCode);
        //删除用户角色
        userInfoMapper.deleteUserRoleInfo(userCode,null);
    }

}
